
/*
 * Vivien2
 *
 * Copyright (c) 2009 Alexei (aka Lancelot) Samsonov.
 * E-mail: alexey.v.samsonov@gmail.com; 
 *
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * Authors: Alexei Samsonov (aka Lancelot), alexey.v.samsonov@gmail.com
 */

/**
 * Date: 
 */



module vivien2;


import std.stdio;
import std.path;
import std.file;

import environment;
import document;
import logger;
import config;


int main(char[][] args)
{
	try
	{
		Document d;
		Environment env;
		char[] file;
		char[] content;
		
		// Получаем имя запрашиваемого пользователем файла из переменных окружения.
		env = new Environment();
		file = env.get("PATH_TRANSLATED");

		// Запрошенный файл должен существовать.
		if(exists(file)==0)
			throw new Exception("File \"" ~ file ~ "\" is not found.");

		// Это должен быть именно файл, а не каталог.
		if(isfile(file)==0)
			throw new Exception("\"" ~ file ~ "\" is not a file.");

		// Читаем файл в переменную.
		content = cast(char[])read(file);

		
		
		char[] ext = getExt(file);
		switch(ext)
		{
			case "htm", "html":
				d = new Html(content);
				break;

			case "txt":
				d = new Text(content);
				break;

			// Тут надо сообщать о неизвестном типе документа.
			default:
				break;

		}

		d.print();
	}
	catch(Exception e)
	{
		Logger log = new Logger();
		log.put("Exception", e.msg);
	}
	catch
	{
		Logger log = new Logger();
		log.put("Error.");
	}

	return 0;
}


